 var util = require("../../utils/util.js"),
    {api} = require("../../utils/api.js"),
     app = getApp();
 Page({
     data: {
         bannerList: [],
         goods_list: [],
         integral_goods_list: [],
         bottom_image: [],
         videoSrc:'',
         userInfo:null,
         couponShow:false
     },
     navMethod(e) {
         let url = e.currentTarget.dataset.url;
         wx.navigateTo({
             url
         })
     },
     onLoad(option) {
        if(option){
            if(util.regularConfig.isString(option.share_id)){
                wx.setStorageSync('share_id', option.share_id)
            }
            if(option.scene){
                const scene = decodeURIComponent(option.scene);
                if(util.regularConfig.isString(scene) && scene.indexOf('=')){
                  let scenes=scene.split('=');
                  if(Array.isArray(scenes) && scenes.length>0){
                    wx.setStorageSync('share_id', scenes[1])
                  }
                }
            }
            if(util.regularConfig.isString(option.coupon_id)){
                api.couponQrcodeDetail(option.coupon_id).then(res=>{
                    console.log('res',res);
                    if(res.data.coupon){
                        this.setData({
                            couponDetail: res.data.coupon,
                            couponShow:true
                        })
                    }
                })
            }
        }
        this.initMethod();
     },
     onClose(){
        this.setData({
            couponShow:false
        })
     },
     async initMethod(){
        let {data}=await api.homeIndex();
        this.setData({ 
            bannerList:data && Array.isArray(data.banner)?data.banner:[],
            goods_list:data && Array.isArray(data.goods_list)?data.goods_list:[],
            integral_goods_list:data && Array.isArray(data.integral_goods_list)?data.integral_goods_list:[],
            bottom_image:data && data.bottom_image?data.bottom_image:'',
            videoSrc:data && data.video?data.video:''
        })
     },
     gotoPage(e){
        let {url,type} = e.currentTarget.dataset;
        if(type == 'tab'){
            wx.switchTab({url})
        }else{
            wx.navigateTo({url})
        }
     },
     goodsDetail(e) {
        let {id,type} = e.currentTarget.dataset;
        wx.navigateTo({
            url:`/pages/shopping/goodsDetail/goodsDetail?id=${id}&type=${type}`
          })
     },
     goodsMore(e){
        let type = e.currentTarget.dataset.type;
        wx.setStorageSync('goodsSwitch', type)
        wx.switchTab({
          url: '/pages/shopping/index/index',
        })
     },
     onShareAppMessage(e) {
         return util.shareEvent(e)
     },
     onReady() {
        this.videoContext = wx.createVideoContext('myVideo')
     },
     onHide(){
        this.videoContext.pause();
     },
     onUnload(){
        this.videoContext.pause();
     },
     onshow(){
        this.videoContext.pause();
     },
     lqsubmit(){
        this.setData({
            couponShow:false
        })
        api.couponLq(this.data.couponDetail.id).then(res=>{
            app.showError('领取成功,请在我的卡券中查看',()=>{})
        })
     }
 })